package com.pai4j.pgc.controller.community;
import com.fasterxml.jackson.core.type.TypeReference;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.config.annotation.ValidateLogin;
import com.pai4j.common.helper.SessionHelper;
import com.pai4j.common.util.JsonUtil;
import com.pai4j.domain.community.vo.response.cc.CTCHomeDataVO;
import com.pai4j.domain.community.vo.response.cc.CTCPathBaseVO;
import com.pai4j.domain.community.vo.response.cc.CourseMenuVO;
import com.pai4j.domain.dto.resource.ResourceDTO;
import com.pai4j.domain.community.vo.DictConfigResponseVO;
import com.pai4j.domain.vo.response.UserBaseResponseInfoVO;
import com.pai4j.pgc.service.community.CCPathHomeService;
import com.pai4j.pgc.service.community.CTCScheduleService;
import com.pai4j.pgc.service.community.DictConfigService;
import com.pai4j.pgc.service.community.StudyHistoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  技术社区首页
 *
 * @author: CYM-pai
 * @date: 2025/10/26 21:50
 **/
@RestController
@RequestMapping("/api/cc/home")
public class CTCHomeController {

    @Autowired
    private CCPathHomeService pathHomeService;

    @Autowired
    private CTCScheduleService scheduleService;

    @Autowired
    private DictConfigService dictConfigService;

    @Autowired
    private StudyHistoryService studyHistoryService;


    @ValidateLogin
    @GetMapping("/study/history")
    public PAIResponseBean<List<ResourceDTO>> history(
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        String userid = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(studyHistoryService.getLastHistoryFromCache(userid, size));
    }

    @GetMapping("/menus/{pathId}")
    public PAIResponseBean<List<CourseMenuVO>> catalogs(@PathVariable Long pathId) {
        DictConfigResponseVO menuDict = dictConfigService.getDictConfigVOListFromCache("CC_MENU", pathId + "");
        String menuJson = menuDict.getValue();
        List<CourseMenuVO> courseMenus = JsonUtil.fromJson(menuJson, new TypeReference<List<CourseMenuVO>>() { });
        return PAIResponseBeanUtil.success(courseMenus);
    }

    /**
     * CC首页聚合接口
     * @param pathId
     * @return
     */
    @GetMapping("/detail/{pathId}")
    public PAIResponseBean<CTCHomeDataVO> detail(@PathVariable Long pathId) {
        String currUserId = SessionHelper.getCurrentUserId();
//        scheduleService.addCTCActivityUserCache(currUserId);
//        scheduleService.addCTCActivityPathCache(pathId);
        return PAIResponseBeanUtil.success(pathHomeService.getHomeDataFromCache(pathId, SessionHelper.getCurrentUserId()));
    }

    @GetMapping("/activityUsers")
    public PAIResponseBean<List<UserBaseResponseInfoVO>> getActivityUser() {
        return PAIResponseBeanUtil.success(scheduleService.getCTCActivityUserFromCache(10));
    }

    @GetMapping("/activityPaths")
    public PAIResponseBean<List<CTCPathBaseVO>> getActivityPath() {
        return PAIResponseBeanUtil.success(scheduleService.getCTCActivityPathFromCache(10));
    }

    @GetMapping("/isAttend")
    public PAIResponseBean<Long> isAttend() {
        String userId = SessionHelper.getCurrentUserId();
        if (StringUtils.isBlank(userId)) {
            return PAIResponseBeanUtil.success(-1);
        }
        boolean isAttend = pathHomeService.isAttend(userId);
        return PAIResponseBeanUtil.success(isAttend ? 1 : 0);
    }
}
